//
//  joymengAdView.m
//  GameChooser
//
//  Created by LT-LT on 14-1-23.
//  Copyright (c) 2014年 moonwolf. All rights reserved.
//

#import "joymengAdView.h"
#import "Admodel.h"
#import "UIButton+WebCache.h"


#define AD_WIDTH self.bounds.size.width/2
#define AD_HEIGHT  self.bounds.size.height/2

@implementation joymengAdView
@synthesize receiveData=_receiveData;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        [self initView];
        
    }
    return self;
}

-(void)initView
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:REQUSET_URL]cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30.0];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) {
                               if ([data length] > 0 && error == nil)
                               {
                                   self.hidden=NO;
                                   NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
                                   [self creatViewwithdata:weatherDic];
                                   
                                   
                               }
                               else if ([data length] == 0 && error ==nil)
                               {
                                   //no data
                                   BarkCallback(NO);
                                   
                               }
                               else if (error != nil)
                               {
                                   //time out
                                   BarkCallback(NO);
                               }
                               else
                                        {
                               }  
                               
                           }];
}

- (void)setCompletionBlock:( void (^) (BOOL success) ) eachBark;
{
    BarkCallback=nil;
    BarkCallback = [eachBark copy];
}

-(void)creatViewwithdata:(NSDictionary *)dic
{
    Admodel *adlist=[[Admodel alloc] initWithAttributes:dic];
    
    if (adlist.popSwitch) {
        UIButton *adbutton=[[UIButton alloc] initWithFrame:CGRectMake(AD_WIDTH/2,AD_HEIGHT/2, AD_WIDTH, AD_HEIGHT)];
        __block UIButton *abutton=adbutton;
        __block UIView *view=self;
        UIButton *closeButton=[[UIButton alloc] initWithFrame:CGRectMake(adbutton.frame.origin.x+adbutton.frame.size.width-10, AD_HEIGHT/2-20, 20, 20)];
        [closeButton setImage:[UIImage imageNamed:@"Colse_Button.png"] forState:UIControlStateNormal];
        [self addSubview:closeButton];
        
        [adbutton setImageWithURL:adlist.popPic forState:UIControlStateNormal placeholderImage:nil completed:
         ^(UIImage *iamge,NSError *error,SDImageCacheType SDImageCacheTypeDisk)
         {
             [view addSubview:abutton];
         }];
        [adbutton setTitle:adlist.popUrl forState:UIControlStateNormal];
        [adbutton addTarget:self action:@selector(adButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [closeButton addTarget:self action:@selector(colseSelf) forControlEvents:UIControlEventTouchUpInside];
        
        BarkCallback(YES);
    }
   
 
}
-(void)colseSelf
{
    [self removeFromSuperview];
}
-(void)adButtonClick:(id)sender
{
    UIButton *button =(UIButton *)sender;
    NSString *str=button.titleLabel.text;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

+(void)localAlertCheck
{
//
//    [[UIApplication sharedApplication] cancelAllLocalNotifications];
//    UILocalNotification *notification=[[UILocalNotification alloc] init];
//    if (notification!=nil) {
//        NSDate *now=[NSDate new];
//        notification.fireDate=[now dateByAddingTimeInterval:LOCAL_SHOW_TIME];
//        notification.repeatInterval=0;
//        notification.timeZone=[NSTimeZone defaultTimeZone];
//        notification.soundName = UILocalNotificationDefaultSoundName;
//        notification.alertBody=@"主人你好久没来人家这er了！";
//        notification.alertAction = @"我来了！";
//        notification.hasAction = YES;
//        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
//    }
}
@end
